import { DatePicker, Form, DatePickerProps } from 'antd';
import { ProFormDateTimePicker } from '@ant-design/pro-form';

type formProps = {
  id?: number;
  name?: string;
  label?: string;
  busType?: number; //{1:用户}
  fieldType?: number; //{1:文本框，2：单选，3：多选，4：日期}
  isDefault?: number;
  isRequire?: number;
  isEnable?: number;
  fieldOption?: string;
  describe?: string;
  createDate?: string;
  order?: number;
  max?: number;
  min?: number;
  regEx?: string;
  precision?: number; //保留几位小数
  disabled?: any;
  fieldProps?: DatePickerProps & any;
  rule?: any;
  placeholder: string;
  dateType: string;
};

export default (props: formProps) => {
  const {
    label,
    isEnable,
    fieldType,
    isDefault,
    name,
    id,
    isRequire,
    regEx,
    min,
    max,
    precision,
    fieldOption,
    disabled,
    fieldProps = {},
    rule = [],
    dateType,
    placeholder,
    ...restProps
  } = props;
  let _name = isDefault ? name : `custom_${id}_${fieldType}`;

  //验证修改
  !!isRequire &&
    rule.push({
      required: true,
      message: `请输入${label}`,
    });
  !isEnable && (rule.length = 0);
  //组件配置修改
  fieldProps.disabled = disabled;
  fieldProps.picker = dateType === 'dateTime' ? 'date' : dateType;

  switch (dateType) {
    case 'dateTime':
      return (
        <ProFormDateTimePicker preserve label={label} name={_name} fieldProps={fieldProps} {...restProps} />
      );

    default:
      return (
        <Form.Item preserve name={_name} rules={rule} label={label} {...restProps}>
          <DatePicker {...fieldProps} placeholder={placeholder}></DatePicker>
        </Form.Item>
      );
  }
};
